package com.example.foo.api.web.product;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.product.model.Bag;
import com.example.foo.product.query.BagQuery;
import com.example.foo.product.service.BagService;

@org.springframework.web.bind.annotation.RestController
public class BagAction {

	private final BagService bagService;

	public BagAction(BagService bagService) {
		this.bagService = bagService;
	}

	@GetMapping("/api/bags")
	public AjaxResult index(BagQuery bagQuery, Pageable pageable) {
		bagQuery.setState(Bag.State.published);
		PageResult<Bag> pageResult = bagService.findBags(bagQuery, pageable);
		return AjaxResult.successData(pageResult);
	}

	@GetMapping("/api/bags/{id}")
	public AjaxResult showBag(@PathVariable("id") Long id) {
		Bag bag = bagService.findBagById(id);
		if (bag == null) {
			return AjaxResult.error(404, "Not Found");
		}
		if (bag.getState() != Bag.State.published) {
			return AjaxResult.error(404, "Not Found");
		}
		return AjaxResult.successData(bag);
	}

}
